package first.group.ssh.channel.session;

import first.group.ssh.channel.Channel;
import first.group.ssh.readBuffer.ReadBuffer;
import java.io.IOException;

public class SessionChannel extends Channel {

    public SessionChannel() {
        super();
    }

    public SessionChannel(int readBufferSize) {
        super(readBufferSize);
    }

    public SessionChannel(int readBufferSize, int extendedBufferSize) {
        super(readBufferSize, extendedBufferSize);
    }

    public SessionChannel(int readBufferSize, int extendedBufferSize, int maxPacketSize) {
        super(readBufferSize, extendedBufferSize, maxPacketSize);
    }

    @Override
    protected String getType() {
        return "session";
    }

    private SessionChannelDataCallback callback;

    public interface SessionChannelDataCallback {

        void readData(byte[] data);

        void readExtendedData(byte[] data);
    }

    public void setCallback(SessionChannelDataCallback callback) {
        this.callback = callback;
    }

    @Override
    protected void readData(ReadBuffer buffer) {
        if (callback == null) {
            throw new IllegalStateException("SessionChannel received data without listener");
        }
        var data = new byte[buffer.getAvaliable()];
        buffer.read(data);
        buffer.consume();
        callback.readData(data);
    }

    @Override
    protected void readExtendedData(ReadBuffer buffer) throws IOException {
        if (callback == null) {
            throw new IllegalStateException("SessionChannel received data without listener");
        }
        var data = new byte[buffer.getAvaliable()];
        buffer.read(data);
        buffer.consume();
        callback.readExtendedData(data);
    }
}
